package com.afinal.intelligencetrash.Model.remote;

import android.os.AsyncTask;

import com.afinal.intelligencetrash.global.GlobalEnvironment;
import com.zhy.http.okhttp.OkHttpUtils;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by cyy on 17-10-13.
 */

public class RTStutasRequester extends AsyncTask<String, String, Void> {

    public static final String REQUEST_RESULT_SUCCESS = "200";
    public static final String REQUEST_RESULT_REDIRECT = "302";
    public static final String REQUEST_RESULT_ERROR = "404";

    private OkHttpClient client;
    private int period = 1000;
    private boolean isOver = false;
    private boolean isPause = false;


    @Override
    protected void onPreExecute() {
        client = OkHttpUtils.getInstance().getOkHttpClient();
        isOver = false;
        isPause = false;
    }


    @Override
    protected Void doInBackground(String... params) {
        while (true){
            if(isOver) break;
            if(!isPause) {
                Request request = getRequest(GlobalEnvironment.URL_GET_RT_STATUS);
                Call call = client.newCall(request);
                Response response = null;
                try {
                    response = call.execute();
                    if(response != null && response.isSuccessful()){
                        if(response.isRedirect()){
                            //服务器不可达
                            publishProgress(REQUEST_RESULT_REDIRECT, "连接不到服务器,请检查网络");
                            continue;
                        }
                        String result = response.body().string();
                        publishProgress(REQUEST_RESULT_SUCCESS, result);
                    }else{
                        //网络错误
                        publishProgress(REQUEST_RESULT_ERROR, "请求失败啊");
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                    //网络错误
                    publishProgress(REQUEST_RESULT_ERROR, "网络错误");
                }finally {
                    if(response != null){
                        response.close();
                    }
                }
            }
            try {
                Thread.sleep(period);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    private Request getRequest(String url) {
        Request request = new Request.Builder()
                .url(url)
                .get().build();
        return request;
    }



    @Override
    protected void onProgressUpdate(String... values) {
        if(resulthandler != null){
            resulthandler.onRequestFinish(values[0], values[1]);
        }
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
    }

    public void cancel(){
        isOver = true;
    }

    @Override
    protected void onCancelled() {
        client = null;
    }

    public void setPeriod(int period) {
        this.period = period;
    }
    private RequestResultHandler resulthandler;

    public void setResulthandler(RequestResultHandler resulthandler) {
        this.resulthandler = resulthandler;
    }

    public void pause() {
        isPause = true;
    }

    public void resume() {
        isPause = false;
    }

    public static interface RequestResultHandler{
        void onRequestFinish(String statusCode, String result);
    }
}
